package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.BrowserActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Event;
import com.eightywork.life.model.MyService;
import com.eightywork.life.model.PmView;
import com.eightywork.life.service.BaseService;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.util.AndroidUtil;
import com.eightywork.life.util.FuncUtil;

public class MyEventListAdapter extends BaseAdapter {

	private int meResource = R.layout.item_service_event;
	private LayoutInflater meInflater;
	private Context meContext;
	private ImageLoader imageLoader;
	private List<Event> list;

	public MyEventListAdapter(Context context, List<Event> list) {
		meContext = context;
		meInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null)?list.size():0;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final Event myEvent = list.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView  = meInflater.inflate(meResource, parent, false);
			holder.username = (TextView)convertView.findViewById(R.id.name);
			holder.name = (TextView)convertView.findViewById(R.id.append_name);
		//	holder.status = (TextView)convertView.findViewById(R.id.tv_state);
			holder.time = (TextView)convertView.findViewById(R.id.time);
			holder.content = (TextView)convertView.findViewById(R.id.content);
			holder.subject = (TextView)convertView.findViewById(R.id.append_title);
			holder.avatar = (ImageView)convertView.findViewById(R.id.avatar);
			holder.verifyType = (ImageView)convertView.findViewById(R.id.verify_type);
			holder.image = (ImageView)convertView.findViewById(R.id.append_image);		
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.content.setText(myEvent.getContent());
		holder.username.setText(myEvent.getUsername());
		holder.name.setText("@" + myEvent.getRealname());
		holder.subject.setText(myEvent.getTitle());
		holder.time.setText(FuncUtil.timeToText(Long.valueOf(myEvent.getDateline()) * 1000L));
		if (myEvent.getPic().startsWith("http:")) {
			imageLoader.DisplayImage(myEvent.getPic(), holder.image, 0, 0, 0);
		} else {
			imageLoader.DisplayImage(BaseService.DOMAIN + myEvent.getPic(), holder.image, 0, 0, 0);
		}	
		int len = AndroidUtil.dpToPx(48, meContext);
		imageLoader.DisplayImage(myEvent.getAvatar_middle(), holder.avatar, 0, len, len);
		holder.verifyType.setBackgroundResource(DataService.getVerifyTypeRes(myEvent.getVerify_type()));
        convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent  = new Intent(meContext, BrowserActivity.class);
				intent.putExtra("url", BaseService.DOMAIN
						+ myEvent.getAppend_link());
				intent.putExtra("idtype", "feed");
			    meContext.startActivity(intent);
			}
		});
		return convertView;
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	public final class ViewHolder {
		private TextView username;
		private ImageView avatar;
		private TextView  status;
		private ImageView image;
		private ImageView verifyType;
		private TextView time;
		private TextView subject;
		private TextView content;
		private TextView name;
		
		
	}

}
